/**
 *  @file Queries.java
 *  @author Mamontov
 *
 *  This file contains Queries class definition
 */
package db21.racerpro;

import java.util.regex.Pattern;

/**
 * @class  Queries
 * @author Mamontov
 *
 *
 */
public class Queries {
    
    public static String read() {
        return "(OWLAPI-readOntology \"$0\" :maintain-owlapi-axioms t)";
    } 
    
    public static String unescapeLocalName(String val) {
        String result = val;
        String from[] = {"__l__", "__g__", "__c__", "__r__", "__c__", "__b__", "__s__",  "_", "__" };
        String to[]  =  {"<"    , ">"    , "^",     "#"    , "%"    , "\""   , "\'"   ,  " ", "_" }; 
        for(int i = 0; i < from.length; i++) {
            result = result.replaceAll(Pattern.quote(from[i]), to[i]);
        } 
        return result;
    }
    
    public static String unescapeValue(String prefix, String val)  {
        // Remove two | at start and end
        String result = val.substring(1);
        result = result.substring(0, result.length() - 1);
        String from[] = {"__l__", "__g__", "__c__", "__r__", "__c__", "__b__", "__s__",  "_", "__", prefix};
        String to[]  =  {"<"    , ">"    , "^",     "#"    , "%"    , "\""   , "\'"   ,  " ", "_" , ""}; 
        for(int i = 0; i < from.length; i++) {
            result = result.replaceAll(Pattern.quote(from[i]), to[i]);
        }        
        return result;
    }
    
    
    public static String escapeValue(String prefix, String val)  {
        String from[] = {"__l__", "__g__", "__c__", "__r__", "__c__", "__b__", "__s__",  "_", "__" };
        String to[]  =  {"<"    , ">"    , "^",     "#"    , "%"    , "\""   , "\'"   ,  " ", "_"  }; 
        String result = val;
        for(int i = from.length - 1; i > -1;i--) {
            result = result.replaceAll(Pattern.quote(to[i]), from[i]);
        }        
        result = prefix + result;
        return result;
    }
    
    public static String sparql() {
        return "(sparql-answer-query \"prefix p: <$0> $1\")";
    }
    
    public static String individualsOfType() {
        return "select ?o  {?o rdf:type  p:$0.}";
    }
    
    public static String getType() {
        return "select ?type {<$0> a ?type}";
    }
    
    public static String getCommonEducationalResourceTypePropertyName() {
        return "common_educational_resource_type_is";
    }
    
    public static String getDigitalEducationalResourceTypePropertyName() {
        return "digital_educational_resource_type_is";
    }
    
    public static String includeThemes() {
        return "include_themes";
    }
    
    public static String createCompetences() {
        return "create_competences";
    }
    
    public static String includedInto() {
        return "included_into";
    }
    
    public static String relatesTo() {
        return "relates_to";
    }
    
    public static String educates() {
        return "educates";
    }
        
    
    public static String includeResources() {
        return "include_resources";
    }
    
    public static String getPropertyValues() {
        return "select ?value {<$0> p:$1 ?value .}";
    }
    
    public static String listSubthemes()  {
        return "select ?theme {?theme p:included_into  <$0>. }";
    }

    public static String resourceByCompetence() {
        return "select  ?resource { ?resource p:create_competences  <$0>. }";
    }

    public static String resourceByTheme() {
        return "select  ?resource { ?resource p:include_themes  <$0>. }";
    }
    
    public static String resourceBySubject() {
        return "select  ?resource { ?resource p:include_themes ?theme. ?theme p:relates_to <$0>. }";
    }
    
    public static String listThemesOfSubject()  {
        String c1 = "{?parenttheme p:relates_to <$0>. ?theme p:included_into ?parenttheme. }";
        //String c2 = "{?theme p:relates_to <$0>. }";
        String c2 = "{?theme rdf:type p:Theme.}";
        return "select distinct ?theme where {" + c1 + " union " + c2 + " }";
    }
    
    public static String listThemesOfSubjectInJena()  {
        return "SELECT DISTINCT ?theme WHERE { { ?parenttheme <$0relates_to> <$1>. ?theme (<$0included_into>)+ ?parenttheme. } UNION { ?theme <$0relates_to> <$1>. }}";
    }
    
    public static String listCompetencesOfSubjectInJena() {
        return "SELECT DISTINCT ?competence WHERE { { ?parenttheme <$0relates_to> <$1>. ?theme (<$0included_into>)+ ?parenttheme. ?theme  <$0educates> ?competence. } UNION { ?theme <$0relates_to> <$1>. ?theme  <$0educates>  ?competence. } }";
    }
    
    public static String themeQueryName() {
        return "theme";
    }
    
    public static String competenceQueryName() {
        return "competence";
    }
}
